package com.egoo.ticket.server.controller;


import com.egoo.ticket.server.pojo.dto.TableHeadFieldDto;
import com.egoo.ticket.server.pojo.vo.TableHeadFieldVo;
import com.egoo.ticket.server.service.HeadFieldService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author donny
 * @since 2020-06-09
 */
@RestController
@RequestMapping("/v1/tableHeadField")
@Api(value = "列表头部字段展示管理",tags = "列表头部字段展示管理",description = "列表头部字段展示管理")
public class HeadFieldController {

	@Autowired
	HeadFieldService tableHeadFieldService;

	@ApiOperation(value = "添加表头信息",notes = "添加表头信息")
	@PostMapping("/insert")
	public DbResponse insert(@RequestBody TableHeadFieldDto tableHeadFieldDto, HttpServletRequest request){
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		if (StringUtils.isEmpty(tableHeadFieldDto.getWorkOrderFilterMenuId())){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"过滤器id不能为空");
		}
		if (CollectionUtils.isEmpty(tableHeadFieldDto.getFieldInfoList())){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"字段编码信息不能为空");
		}
		tableHeadFieldDto.setTenantId(tenantId);
		Integer insert = tableHeadFieldService.insert(tableHeadFieldDto);
		if (insert >0){
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.ADD_SUCCESS_MSG);
		}
		return new DbResponse(ResponseConstant.RES_FAIL_CODE,ResponseConstant.ADD_FAIL_MSG);
	}


	@ApiOperation(value = "查询表头信息",notes = "查询表头信息")
	@PostMapping("/select")
	public DbResponse select(@RequestBody TableHeadFieldDto tableHeadFieldDto, HttpServletRequest request){
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		if (StringUtils.isEmpty(tableHeadFieldDto.getWorkOrderFilterMenuId())){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"过滤器id不能为空");
		}
		tableHeadFieldDto.setTenantId(tenantId);
		ArrayList<TableHeadFieldVo> tableHeadFieldVos = tableHeadFieldService.select(tableHeadFieldDto);
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.SELECT_SUCCESS_MSG,tableHeadFieldVos);
	}

	@ApiOperation(value = "修改表头信息",notes = "修改表头信息")
	@PostMapping("/update")
	public DbResponse update(@RequestBody TableHeadFieldDto tableHeadFieldDto, HttpServletRequest request){
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		if (StringUtils.isEmpty(tableHeadFieldDto.getWorkOrderFilterMenuId())){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"过滤器id不能为空");
		}
		if (CollectionUtils.isEmpty(tableHeadFieldDto.getFieldInfoList())){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"字段编码信息不能为空");
		}
		tableHeadFieldDto.setTenantId(tenantId);
		Integer update = tableHeadFieldService.update(tableHeadFieldDto);
		if (update >0){
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.UPDATE_SUCCESS_MSG);
		}

		return new DbResponse(ResponseConstant.RES_FAIL_CODE,ResponseConstant.UPDATE_FAIL_MSG);
	}

}
